<!-- 显示数据的列表 -->
<template>
  <div class="system-content-list-data-list">
    <el-table ref="multipleTable" :data="dataTable" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" @select-all="selectAll">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="FAQ名称" prop="faqTitle" class-name="cusxx">
        <template slot-scope="scope">
          <TooltipOver :tipValue="scope.row.faqTitle" :refTipName="'ref-title-' + scope.row.faqId"/>
        </template>
      </el-table-column>

      <el-table-column prop="categoryName" label="FAQ分类" width="180"
        v-if="system && system.systemType && system.systemType.id === 6">
        <template slot-scope="scope">
          <span v-if="scope.row.categoryId">{{scope.row.categoryName}}</span>
          <span v-else style="color:#999999;">未定义</span>
        </template>
      </el-table-column>

      <el-table-column label="显示否" prop="isShow" width="100">
        <template slot-scope="scope">
          <el-switch v-model="isShow[scope.row.faqId]" active-color="#13ce66"
            @change="changeShow(scope.row.faqId, isShow[scope.row.faqId])">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="releasedAt" label="发布时间" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          {{scope.row.releasedAt | dateformat}}
        </template>
      </el-table-column>
      <el-table-column prop="createAt" label="创建时间" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          {{scope.row.createAt | dateformat}}
        </template>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip width="210">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isTop > 0" @click="checkTopItem(scope.row.faqId)" type="text"><span
              class="orange">取消置顶</span></el-button>
          <el-button v-if="scope.row.isTop < 1" @click="checkTopItem(scope.row.faqId)" type="text"><span
              class="green">置顶</span></el-button>
          <el-button @click="checkItem(scope.row.faqId)" type="text">查看</el-button>
          <el-button @click="eidtItem(scope.row.faqId)" type="text">编辑</el-button>
          <el-button @click="deleteArticle(scope.row.faqId)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import TooltipOver from '@/components/common/TooltipOver.vue'
import { getSystemItemPathBySystemAimId } from '@/core/functions.js'
export default {
  props: {
    dataTable: {
      type: Array
    },
    multipleSelection: {
      type: Array
    },
    multipleSelectionChange: {
      type: Function
    },
    resetData: {
      type: Function
    },
    system: {
      type: Object
    },
    page: {
      type: Object
    }
  },
  data () {
    return {
      isShow: {}
    }
  },
  watch: {
    dataTable: {
      handler (newValue) {
        if (newValue.length > 0) {
          newValue.forEach(item => {
            this.isShow[item.faqId] = item.isShow > 0
          })
        } else {
          this.isShow = {}
        }
      },
      deep: true
    }
  },
  methods: {
    // 批量删除选中回调方法
    handleSelectionChange (val) {
      this.multipleSelectionChange(val)
    },
    selectAll (val) {
      if (!val.length && !this.dataTable.length) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    checkTopItem (id) {
      let item = this.dataTable.find((element) => {
        return element.faqId === id
      })
      if (!item) {
        return
      }
      let param = {
        id: id,
        isTop: item.isTop < 1 ? 1 : 0
      }
      // 请求后台修改状态
      this.$post('/system/faq/change-istop', param).then(response => {
        if (response && response > 0) {
          this.resetData()
        }
      })
    },
    changeShow (id, isShow) {
      let param = {
        id: id,
        isShow: isShow ? 1 : 0
      }
      // 请求后台修改状态
      this.$post('/system/faq/change-show', param).then(response => {
        if (response && response > 0) {
          let show = this.isShow
          show[id] = isShow
          this.isShow = JSON.parse(JSON.stringify(show))
        }
      })
    },
    // 查看
    checkItem (id) {
      let item = this.dataTable.find((element) => {
        return element.faqId === id
      })
      if (!item) {
        return
      }
      // 通过id 获取对应的url链接，然后打开链接
      this.$post('/url/system-item-link', {
        systemId: this.system.id,
        itemId: id
      }).then((response) => {
        if (response && response.length > 0) {
          window.open(response)
        }
      })
    },
    // 编辑
    eidtItem (id) {
      let item = this.dataTable.find((element) => {
        return element.faqId === id
      })
      if (!item) {
        return
      }
      let fullPath = getSystemItemPathBySystemAimId(this.system, item.faqId)
      if (!fullPath) {
        return
      }
      // 存储列表页码
      sessionStorage.setItem('systbPageCur' + this.system.id, JSON.stringify({pageCur: this.page.page, fromItem: false}))
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 删除
    deleteArticle (id) {
      let item = this.dataTable.find((element) => {
        return element.faqId === id
      })
      if (!item) {
        return
      }

      this.$confirm('确认要删除 ' + item.faqTitle + ' FAQ？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.$post('/system/faq/delete', {
            id: id
          }).then((response) => {
            if (response && response.id && parseInt(response.id) > 0) {
              this.resetData()
            }
          })
        })
        .catch(() => {
        })
    }
  },
  components: {
    TooltipOver
  }
}

</script>

<style scoped lang="scss">
.system-content-list-data-list /deep/ {
  .el-table th {
    color: #555;
  }
  .el-table-column--selection .cell {
    padding-left: 10px;
  }
}
</style>
